﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Windows.Forms;

namespace OtherDevice
{
    [Description("温度传感器-SM3720B")]
    class Temperature_SM3720B:Temperature_Base
    {
        public Temperature_SM3720B()
        {
            controlFrom = new Form_测温();
        }
        public delegate void EventGetTemperatureDelegate(double d_Value);
        public event EventGetTemperatureDelegate EventGetTemperature = null;
        private double d_ActTemperature = 0;
        private double d_Act湿度 = 0;
        bool b_Reflush = false;
        public override void InitDeal()
        {
            m_SerialPort.DataReceived += M_SerialPort_DataReceived;
        }
        /// <summary>
        /// 数据接收
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                byte[] byte_Result = new byte[1024];
                ReceiveData(ref byte_Result, 6000);
                string strReceived = ArrayByteToHexStr(byte_Result);
                //将收到的十六进制字符串 strReceived 以空格拆分成数组
                string[] arrayStrReceived = strReceived.Split(' ');
                string value1 = arrayStrReceived[3].ToString() + arrayStrReceived[4].ToString();
                double Value1 = Convert.ToInt32(value1, 16);
                d_ActTemperature = Value1 / 100;
                string value2 = arrayStrReceived[5].ToString() + arrayStrReceived[6].ToString();
                double Value2 = Convert.ToInt32(value2, 16);
                d_Act湿度 = Value2 / 100;
                b_Reflush = true;
                if (EventGetTemperature != null)
                    EventGetTemperature(d_ActTemperature);
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取温度失败" + ex.Message);
            }
        }

        //发送锁
        object objSendSensorLock = new object();
        /// <summary>
        /// 查询温度
        /// </summary>
        /// <returns></returns>
        public override bool Function_查询温度(ref double d_Temperature, int n_TimeOut = 3000)
        {
            lock (objSendSensorLock)
            {
                try
                {
                    byte[] bt = new byte[8];
                    bt[0] = 1;
                    bt[1] = 3;
                    bt[2] = 0;
                    bt[3] = 0;
                    bt[4] = 0;
                    bt[5] = 2;
                    bt[6] = 196;
                    bt[7] = 11;
                    b_Reflush = false;
                    m_SerialPort.Write(bt, 0, bt.Length);
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Restart();
                    while (true)
                    {
                        if (stopWatch.ElapsedMilliseconds > 3000)
                            return false;
                        if (b_Reflush)
                            break;
                        Thread.Sleep(50);
                    }
                    d_Temperature = d_ActTemperature;
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发送获取温度指令失败" + ex.Message);
                    return false;
                }
            }

        }
        public override bool Function_查询湿度(ref double d_湿度, int n_TimeOut = 3000)
        {
            lock (objSendSensorLock)
            {
                try
                {

                    byte[] bt = new byte[8];
                    bt[0] = 1;
                    bt[1] = 3;
                    bt[2] = 0;
                    bt[3] = 0;
                    bt[4] = 0;
                    bt[5] = 2;
                    bt[6] = 196;
                    bt[7] = 11;
                    b_Reflush = false;
                    m_SerialPort.Write(bt, 0, bt.Length);
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Restart();
                    while (true)
                    {
                        if (stopWatch.ElapsedMilliseconds > 3000)
                            return false;
                        if (b_Reflush)
                            break;
                        Thread.Sleep(50);
                    }
                    d_湿度 = d_Act湿度;
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发送获取温度指令失败" + ex.Message);
                    return false;
                }
            }
        }

    }
}
